//表单验证配置文件
let validateFormOpt = {
    fieldsOpt: {
        email: {
            rule: { reg: /^\w{2,}@\w{2,}\.[A-Za-z0-9]{2,}$/ },
            successMsg: " ",
            errorMsg: "没有邮箱吗",
            selector: '#email'
        },
        tel: {
            rule: { reg: /^1[2-9]\d{9}$/ },
            successMsg: " ",
            errorMsg: "电话号码格式不正确",
            selector: '#tel'
        },
        username: {
            rule: { reg: /^[a-zA-Z]\w{5}/ },
            successMsg: " ",
            errorMsg: "用户名格式不正确",
            selector: '#username'
        },
        password1: {
            rule: { reg: /^.{6,20}$/ },
            successMsg: " ",
            errorMsg: "密码长度为6-20位",
            selector: '#password1'
        },
        password2: {
            rule: { reg: /^.{6,20}$/, equal: 'password1',value:"abc" },
            successMsg: " ",
            errorMsg: "两次密码不一样",
            selector: '#password2'
        },
        // agree:{
        //     rule:{checked:true},
        //     selector: '#agree'
        // }
    },
    submit: {
        selector: '#submit'
    },
    //自定义字段验证成功失败函数
    valiField: function (field, valiRes) {
        console.log('进入自带那个也')
        let curIpt = document.querySelector(this.fieldsOpt[field].selector)
        let infoSpan = curIpt.nextElementSibling
        if (valiRes) {
            curIpt.parentElement.classList.add('has-success')
            curIpt.parentElement.classList.remove('has-error')
            infoSpan.innerText = this.fieldsOpt[field].successMsg
            console.log(`${field}验证成功`)
        } else {
            curIpt.parentElement.classList.remove('has-success')
            curIpt.parentElement.classList.add('has-error')
            infoSpan.innerText = this.fieldsOpt[field].errorMsg
            console.log(`${field}验证失败`)
        }
    },
    //自定义表单验证成功回掉函数
    success: function (e) {
        e.preventDefault()
        console.log('验证成功')
    },
    //自定义表单验证成功失败回掉函数
    error: function (e) {
        e.preventDefault()
        console.log('验证失败')
    }
}


let vf = new ValidateForm(validateFormOpt)
//开启表单验证
vf.validate()